function lookupDictionary(word) {
	$.getJSON("http://localhost:8080/writeonproject/dictionary?word=" + word, parseDefinition);
}

function parseDefinition(data) {
	var xml = data.definition;
	path="//Dictionary/Name[text()='WordNet (r) 2.0']/../../WordDefinition/text()";
	var def = "";
	
	xml = xml.substring(xml.indexOf('>')+1, xml.lastIndexOf('>')+1);
	xml = "<WordDefinition" + xml.substring(xml.indexOf('>'));
	
	if (window.DOMParser) {
	  parser=new DOMParser();
	  var xmlDoc=parser.parseFromString(xml,"text/xml");
	  
	  var nodes=xmlDoc.evaluate(path, xmlDoc, null, XPathResult.ANY_TYPE, null);
	  var result=nodes.iterateNext();
	  if (result) {
			def=result.nodeValue;
	  }
	}
//	else // Internet Explorer
//	  {
//	  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
//	  xmlDoc.async="false";
//	  xmlDoc.loadXML(xml);
//	  } 
	else {
		alert("Dictionary function not supported for Internet Explorer.");
	}
	
	$("#dictionary_box").html(formatDefinition(def));
}

function formatDefinition(def) {
	def = "<b>" + def.substring(0, def.indexOf(" ")) + "</b>" + def.substring(def.indexOf(" "));
	
	var nums = def.match(/d+:/g);
	for (num in nums) {
		def = def.replace(num, "<br><br>" + num);
	}
	
	return def;
}

$(document).ready(function () {
	$("#lookup_box").keypress(function(event){
		if(event.which == 13){
			lookupDictionary($('#lookup_box').val());
		}
	});
});
